/**
 *
 * History:
 */
package com.powersi.biz.util.report.xls.impl;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jxl.Workbook;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

import org.apache.log4j.Logger;

/**
 * 生成execl报表辅助类
 */

public class ExcelSupport {
	private static Logger log = Logger.getLogger("ExcelSupport");
	
	/**
	 * 得到excel对象
	 * @param fileName           execl文件名
	 * @return                   excel对象
	 */
	public static WritableWorkbook getWorkbook(String fileName,Workbook workbook,HttpServletResponse response){
		try{
			fileName = new String(fileName.getBytes("GBK"),"ISO8859-1");
		}catch(UnsupportedEncodingException e){
			log.warn("Encoding fileName has a exception: " + e);
		}
		//清空输出流
		response.reset();
		//设定输出文件头
	    response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
	    //定义输出类型
	    response.setContentType("application/msexcel");
	    try{
	    	OutputStream os = response.getOutputStream();
			return Workbook.createWorkbook(os,workbook);
	    }catch(IOException e){
	    	log.warn("Create outputStream has a IOException :"+ e);
	    }
	    return null;
	}
	
	/**
	 * 得到excel对象
	 * @param fileName           execl文件名
	 * @return                   excel对象
	 */
	public static WritableWorkbook getWorkbook(String fileName,HttpServletResponse response){
		try{
			fileName = new String(fileName.getBytes("GBK"),"ISO8859-1");
		}catch(UnsupportedEncodingException e){
			log.warn("Encoding fileName has a exception: " + e);
		}
		//清空输出流
		response.reset();
		//设定输出文件头
	    response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
	    //定义输出类型
	    response.setContentType("application/msexcel");
	    try{
	    	OutputStream os = response.getOutputStream();
			return Workbook.createWorkbook(os);
	    }catch(IOException e){
	    	log.warn("Create outputStream has a IOException :"+ e);
	    }
	    return null;
	}
	/**
	 * 得到excel对象
	 * @param fileName           execl文件名
	 * @return                   excel对象
	 */
	public static WritableWorkbook getWorkbook(String fileName,String tempName,HttpServletRequest request,HttpServletResponse response){
		try{
			fileName = new String(fileName.getBytes("GBK"),"ISO8859-1");
		}catch(UnsupportedEncodingException e){
			log.warn("Encoding fileName has a exception: " + e);
		}
		
		//清空输出流
		response.reset();
		//设定输出文件头
	    response.setHeader("Content-disposition", "attachment; filename="+fileName+".xls");
	    //定义输出类型
	    response.setContentType("application/msexcel");
	    try{
	    	OutputStream os = response.getOutputStream();
			return Workbook.createWorkbook(os);
	    }catch(IOException e){
	    	log.warn("Create outputStream has a IOException :"+ e);
	    }
	    return null;
	}
	/**
	 * 写出execl对象
	 * @param workbook              execl对象
	 */
	public static void writeWorkbook(WritableWorkbook workbook){
		try{
			workbook.write();
			workbook.close();
		}catch(IOException e){
			log.warn("Create outputStream has a IOException :"+ e);
		}catch(WriteException e){
			log.warn("Create outputStream has a WriteException :"+ e);
		}
		
	}

}

